package com.aurora.oa.work.presenter;


import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.message.bean.ResponseFileID;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.StringUtils;
import com.aurora.oa.work.bean.AddExpenceData;
import com.aurora.oa.work.bean.ApplyMoneyData;
import com.aurora.oa.work.view.AddExpenceActivityView;
import com.aurora.oa.work.view.ApplyMoneyActivityView;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Administrator on 2017/04/28
*/

public class ApplyMoneyActivityPresenterImpl implements ApplyMoneyActivityPresenter{
    ApplyMoneyActivityView view;
    RequestHttpModel model;
    //上传成功服务器返回ID
    List<File> files;
    List<String> fileIDs;
    private ApplyMoneyData applyMoneyData;
    public ApplyMoneyActivityPresenterImpl(ApplyMoneyActivityView view) {
        this.view = view;
        model = new RequestHttpModelImpl();
    }

    @Override
    public void applyMoney(){
        //获取要发送的数据
        applyMoneyData= view.getApplyMoneyData();
        //为空就不发送消息
        if(checkMessageNull()){
            return;
        }

        view.showProgress();
        files = view.getImageFiles();
        if(files.size()==0){
            //发送没图片的申请数据
            Map<String, String> params = new HashMap<>();
            params.put("content", applyMoneyData.getContent()==null?"":applyMoneyData.getContent());
            params.put("money", applyMoneyData.getMoney()==null?"":applyMoneyData.getMoney());
            params.put("partner", applyMoneyData.getPartner()==null?"":applyMoneyData.getPartner());
            params.put("attachmentids", applyMoneyData.getAttachmentids()==null?"":applyMoneyData.getAttachmentids());
            params.put("remark", applyMoneyData.getRemark()==null?"":applyMoneyData.getRemark());
            params.put("approvers", applyMoneyData.getApprovers()==null?"":applyMoneyData.getApprovers());

            model.postForm(AuroraConstants.SEND_MONEY_APPLY,params,new OnRequestHttpDataListener(){

                @Override
                public void onRequestDataSuccess(String body) {

                    Gson gson = new Gson();
                    HttpResult result = gson.fromJson(body, HttpResult.class);
                    view.hideProgress();
                    if(result!=null&&result.success){
                        view.onApplyMoneySuccess();
                    }else{
                        view.onApplyMoneyFail(result.msg);
                    }



                }

                @Override
                public void onRequestDataFail(String message) {
                    view.hideProgress();
                    view.onApplyMoneyFail(message);
                }
            });
            return;
        }
        fileIDs = new ArrayList<>();
        for(int i = 0 ;i<files.size();i++){
            model.postFile(AuroraConstants.POST_FILE_URL, files.get(i), new OnRequestHttpDataListener() {
                @Override
                public void onRequestDataSuccess(String body) {
                    Gson gson = new Gson();
                    ResponseFileID idData = gson.fromJson(body,ResponseFileID.class);
                    if(idData!=null&&idData.isSuccess()){
                        fileIDs.add(idData.getMsg());
                        //图片上传完成
                        if(fileIDs.size()==files.size()){
                            //开始上传数据

                            applyMoneyData.setAttachmentids(StringUtils.getStringFromList(fileIDs));
                            Map<String, String> params = new HashMap<>();
                            params.put("content", applyMoneyData.getContent()==null?"":applyMoneyData.getContent());
                            params.put("money", applyMoneyData.getMoney()==null?"":applyMoneyData.getMoney());
                            params.put("partner", applyMoneyData.getPartner()==null?"":applyMoneyData.getPartner());
                            params.put("attachmentids", applyMoneyData.getAttachmentids()==null?"":applyMoneyData.getAttachmentids());
                            params.put("remark", applyMoneyData.getRemark()==null?"":applyMoneyData.getRemark());
                            params.put("approvers", applyMoneyData.getApprovers()==null?"":applyMoneyData.getApprovers());

                            model.postForm(AuroraConstants.SEND_MONEY_APPLY,params,new OnRequestHttpDataListener(){

                                @Override
                                public void onRequestDataSuccess(String body) {

                                    Gson gson = new Gson();
                                    HttpResult result = gson.fromJson(body, HttpResult.class);
                                    view.hideProgress();
                                    if(result!=null&&result.success){
                                        view.onApplyMoneySuccess();
                                    }else{
                                        view.onApplyMoneyFail(result.msg);
                                    }



                                }

                                @Override
                                public void onRequestDataFail(String message) {
                                    view.hideProgress();
                                    view.onApplyMoneyFail(message);
                                }
                            });
                        }
                    }else{
                        view.hideProgress();
                        view.onApplyMoneyFail(idData.getMsg());
                    }
                }

                @Override
                public void onRequestDataFail(String message) {
                    view.hideProgress();
                    view.onApplyMoneyFail(message);

                }
            });
        }

    }

    //非空校验
    private boolean checkMessageNull() {

        if(StringUtils.isNullString(applyMoneyData.getContent())){
            view.onApplyMoneyFail("申请内容不能为空");
            return true;
        }

        if(StringUtils.isNullString(applyMoneyData.getMoney())){
            view.onApplyMoneyFail("申请金额不能为空");
            return true;
        }

        if(Double.parseDouble(applyMoneyData.getMoney())<0){
            view.onApplyMoneyFail("金额不能小于0");
            return true;
        }

        if(StringUtils.isNullString(applyMoneyData.getPartner())){
            view.onApplyMoneyFail("合作对象不能为空");
            return true;
        }
        return false;
    }
}